package com.oa.hyperion.api.v1;

import com.oa.hyperion.entity.assets.AssetsRepair;
import com.oa.hyperion.entity.assets.AssetsGeneralLedger;
import com.oa.hyperion.entity.assets.AssetsRepair;
import com.oa.hyperion.entity.assets.AssetsScrap;
import com.oa.hyperion.service.AssetsRepairService;
import com.oa.hyperion.service.AssetsGeneralLedgerService;
import com.oa.hyperion.service.AssetsRepairService;
import com.oa.hyperion.service.AssetsScrapService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.quartz.DailyTimeIntervalScheduleBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 资产报修controller
 * author yuanchongwei
 * date 2022-1-21
 */
@Api(value="资产报修controller",tags={"资产报修操作接口"})
@RestController
@RequestMapping("/v1/assets/repair")
public class AssetsRepairController {
    @Autowired
    private AssetsGeneralLedgerService assetsService;
    @Autowired
    private AssetsRepairService assetsRepairService;
    @Autowired
    private AssetsScrapService assetsScrapService;

    @ApiOperation("资产报修")
    @PostMapping("add")
    public String addAssetsRepair(@RequestBody AssetsRepair assetsRepair){
        //1.先判断资产当前状态是否=3,  2.=3时，判断资产是否已在报修表中存在， 以及维修结果是否为维修中，！=3，可添加
        //根据资产id查询总账表中的资产信息
        AssetsGeneralLedger agl = assetsService.getAssetsById(assetsRepair.getAssetId());
        //查询资产当前状态
        if (agl.getStatus() == 3) {
            //判断报修中表的维修结果,2种情况：维修中，维修完成
            List<AssetsRepair> asrs = assetsRepairService.getAssetsRepairByAssetId(assetsRepair.getAssetId());
            String resultLatest = assetsRepairService.getAssetsRepairByResultLatest(assetsRepair.getAssetId());

            if (asrs == null ) {
                //维修表中为空，则可以添加
                assetsRepairService.insert(assetsRepair);
                return "信息异常，报修表中无该资产信息，已自动添加";
            }
            //依次判断最近一次的维修结果
            if(resultLatest == null){
                //无记录
                return "信息异常";
            }
            else if (resultLatest.equals("1")) {
                //有存在维修结果=1的结果，说明资产已在维修中
                return "资产已在维修中，不可重复提交，请耐心等等!";
            }
            else if(resultLatest.equals("0")){
                //修复成功，将状态改为可用
                agl.setStatus(1);
                assetsService.update(agl);
                return "资产维修成功后状态未更新，已自动更新";
            }
            else{
                //修复失败，将资产报废
                agl.setStatus(4);
                if(assetsService.update(agl)){
                    AssetsScrap assetsScrap = new AssetsScrap();
                    assetsScrap.setAssetId(assetsRepair.getAssetId());
                    assetsScrap.setReason("修复失败");
                    assetsScrap.setScrapDate(new Date());
                    assetsScrap.setHandler(assetsRepair.getResponser());
                    assetsScrapService.insert(assetsScrap);
                    return "资产维修失败后未报废，已自动更新";
                }
                else{
                    return "资产维修完成后状态未更新，请手动更新";
                }
            }
        }
        else if (agl.getStatus() == 4) {
            return "资产已报废";
        }
        // 原来的修改次数
        int repairtimesBefore= agl.getRepairTimes();
        //1.写入报修表
        assetsRepairService.insert(assetsRepair);
        //2.修改总账表中的修改次数，状态
        agl.setRepairTimes(repairtimesBefore+1);
        agl.setStatus(3);
        if(assetsService.update(agl)){
            return "操作成功";
        }
        else{
            return "操作失败";
        }

    }


    @ApiOperation("上传维修结果")
    @PostMapping("update")
    public String updateRepairResult(@RequestBody AssetsRepair assetsRepair){
        if (assetsRepair == null && assetsRepair.getResult().equals("") || assetsRepair.getResult() == null) {
            return "请确认参数不能为空";
        }
        AssetsGeneralLedger agl = assetsService.getAssetsById(assetsRepair.getAssetId());
        //判断资产当前状态是否=3
        if(agl.getStatus()!=3){
            return "资产并未在维修清单，请确认";
        }

        String resultToWrite = assetsRepair.getResult();
        //上传维修结果
        assetsRepairService.update(assetsRepair.getId(),resultToWrite);
        //判断结果：成功 or 失败
        if(resultToWrite.equals("0")){
            //同步修改总账表的状态
            agl.setStatus(1);
            if(assetsService.update(agl)){
                return "资产状态更新成功:可用";
            }
            else{
                return "产状态更新失败，请手动更新";
            }
        }
        else if(resultToWrite.equals("2")){
            //同步修改总账表的状态
            agl.setStatus(4);
            if(assetsService.update(agl)){
                AssetsScrap assetsScrap = new AssetsScrap();
                assetsScrap.setAssetId(assetsRepair.getAssetId());
                assetsScrap.setReason("修复失败");
                assetsScrap.setScrapDate(new Date());
                assetsScrap.setHandler(assetsRepair.getResponser());
                assetsScrapService.insert(assetsScrap);
                return "因为资产维修失败已自动申请报废";
            }
            else{
                return "资产维修完成后状态未更新，请手动更新";
            }
        }
        else{
            return "维修中。。。";
        }

    }


    @ApiOperation("查询所有报修记录")
    @GetMapping("all")
    public List<AssetsRepair> getAllRepairInfo(){
        return assetsRepairService.getAllRepairInfo();
    }

    @ApiOperation("根据资产id查询报修信息")
    @GetMapping("assetid/{assetId}")
    public List<AssetsRepair> getRepairByAssetId(@PathVariable int assetId){
        if (assetId == 0) {
            return null;
        }
        return assetsRepairService.getAssetsRepairByAssetId(assetId);
    }

    @ApiOperation("根据申请人查询报废信息")
    @GetMapping("applyer/{applyer}")
    public List<AssetsRepair> getRepairBy(@PathVariable String applyer){
        if (applyer.equals("") || applyer == null) {
            return null;
        }
        return assetsRepairService.getAssetsRepairByApplyer(applyer);
    }

    @ApiOperation("根据负责人查询报修信息")
    @GetMapping("responser/{responser}")
    public List<AssetsRepair> getRepairByTransferOut(@PathVariable int responser){
        if (responser == 0) {
            return null;
        }
        return assetsRepairService.getAssetsRepairByResponser(responser);
    }

    @ApiOperation("根据维修结果查询报修信息")
    @GetMapping("result/{result}")
    public List<AssetsRepair> getRepairByTransferIn(@PathVariable int result){
        if (result < 0) {
            return null;
        }
        return assetsRepairService.getAssetsRepairByResult(result);
    }


}
